﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EvaluationSystem.Exceptions;
using System.Data;
using System.Web.UI;

namespace EvaluationSystem.DAO
{
    public class EvaluationItem
    {
        public int Id { get; set; }
        public string SummaryName { get; set; }
        public string Evaluation { get; set; }
    }
    public class EvaluationItemsManager : DAO
    {
        public bool AddEvaluationItem(string summaryId, string evaluation)
        {
            if (CheckExistEvaluationByName(evaluation))
            {
                throw new ExistEvaluationException();
            }
            string AddEvaluationCommand = "insert into [EvaluationItems] (SummaryId, Evaluation) values(@SummaryId, @Evaluation)";
            Dictionary<string, object> parameters = new Dictionary<string, object>(2);
            parameters["SummaryId"] = summaryId;
            parameters["Evaluation"] = evaluation;
            return ExcuteCommandNonQuery(AddEvaluationCommand, parameters) > 0;
        }

        public bool CheckExistEvaluationByName(string evaluationName)
        {
            string selectEvaluationByNameCommand = "select [Id] from [EvaluationItems] where [Evaluation]=@Evaluation";
            Dictionary<string, object> parameters = new Dictionary<string, object>(1);
            parameters["Evaluation"] = evaluationName;
            return ExcuteCommandDataTable(selectEvaluationByNameCommand, parameters).Rows.Count > 0;
        }

        public DataTable GetAllEvaluationItems()
        {
            string selectAllEvaluation = "select [EvaluationItems].[Id], [Summary].[SummaryName], [Evaluation] from [Summary],[EvaluationItems] where [EvaluationItems].[SummaryId] = [Summary].[Id]";
            return ExcuteCommandDataTable(selectAllEvaluation, null);
        }

        public int GetEvaluationItemsCount(Page page, bool isRefresh = false)
        {
            if (page.Session[Constants.EvaluationCount] != null && !isRefresh)
            {
                return (int)page.Session[Constants.EvaluationCount];
            }
            else
            {
                string getEvaluationItemsCountCommand = "select count([Id]) from [EvaluationItems] ";
                DataTable dt = ExcuteCommandDataTable(getEvaluationItemsCountCommand, null);
                int count = 0;
                if (dt.Rows.Count > 0)
                {
                    count = (int)dt.Rows[0][0];
                }
                return count;
            }
        }

        public bool DeleteEvaluationById(string Id)
        {
            string deleteEvaluationByIdCommand = "delete [EvaluationItems] where [Id]=@Id";
            Dictionary<string,object> parameters = new Dictionary<string,object>(1);
            parameters["Id"] = Id;
            return ExcuteCommandNonQuery(deleteEvaluationByIdCommand, parameters) > 0;
        }

        public bool UpdateEvaluationById(string id, string summaryId, string evaluation)
        {
            if (CheckExistEvaluationByIdExceptMe(id,evaluation))
            {
                throw new ExistEvaluationException();
            }
            string updateEvaluationByIdCommand = "update [EvaluationItems] set [SummaryId]=@SummaryId, [Evaluation]=@Evaluation where [Id]=@Id";
            Dictionary<string, object> parameters = new Dictionary<string, object>(3);
            parameters["SummaryId"] = summaryId;
            parameters["Evaluation"] = evaluation;
            parameters["Id"] = id;
            return ExcuteCommandNonQuery(updateEvaluationByIdCommand, parameters) > 0;
        }

        protected bool CheckExistEvaluationByIdExceptMe(string id, string evalaution)
        {
            string selectSummaryExceptMeCommand = "select [Id] from [EvaluationItems] where [Evaluation]=@Evaluation and [Id]<>@Id";
            Dictionary<string, object> parameters = new Dictionary<string, object>(2);
            parameters["Evaluation"] = evalaution;
            parameters["Id"] = id;
            return ExcuteCommandDataTable(selectSummaryExceptMeCommand, parameters).Rows.Count > 0;
        }
    }
}